package com.sdry.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.SysPosition;
import com.sdry.system.service.PositionService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: info_sdry
 * @description: 岗位
 * @author: cb
 * @create: 2020-02-18 11:39
 **/
@Controller
@RequestMapping(value = "/post")
public class PositionController extends BaseController {

    @Autowired
    private PositionService positionService;

    /**
     * 展示
     * @return
     */
    @RequestMapping("/showPosition")
    public String showPosition() {
        return "system/position/positionList";
    }

    /**
     * 分页查询
     * @return
     */
    @RequestMapping(value = "showPostPage")
    @ResponseBody
    public TableDataInfo showPostPage(SysPosition sysPosition) {
        logger.info("获取岗位列表 sysPosition = " + JSONObject.fromObject(sysPosition));
        Page<SysPosition> page = new Page<>(sysPosition.getPage(),sysPosition.getLimit());
        QueryWrapper<SysPosition> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(sysPosition.getPostName())) {
            queryWrapper.eq("del_flag",0L);
        }else {
            queryWrapper.eq("del_flag",0L).like("post_name", sysPosition.getPostName());
        }
        IPage<SysPosition> sysPositionIPage = positionService.page(page,queryWrapper);
        return getDataTable(sysPositionIPage.getRecords(), sysPositionIPage.getTotal());
    }

    /**
     * 新增
     * @param sysPosition
     * @return
     */
    @RequestMapping(value = "addPosition")
    @ResponseBody
    public boolean addPosition(SysPosition sysPosition) {
        return positionService.saveOrUpdate(sysPosition);
    }

    /**
     * 去编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "updatePosition" )
    @ResponseBody
    public String updatePosition(Long id) {
        SysPosition sysPosition = null;
        if (id != null){
            sysPosition = positionService.getById(id);
        }
        return JSONObject.fromObject(sysPosition).toString();
    }

    /**
     * 删除
     * @param postId 主键ID
     * @return
     */
    @RequestMapping(value = "deletePosition")
    @ResponseBody
    public boolean deletePosition(Long postId) {
        return positionService.deletePosition(postId);
    }

}
